package response
import (
    "fmt"
    "net/http"
	"github.com/gin-gonic/gin"
	"ivr/robot"
	"ivr/session"
	"runtime"
	//"ivr/nlg"
)

const (
	CALLIN="callin"
	CHATTING="input"
	CALLEND="hangup"
)
type InputMsg struct {
	Uuid    string `form:"uuid" json:"uuid" xml:"uuid"  `
	RobotId string `form:"robotId" json:"robotId" xml:"robotId"  binding:"required"`
	Cmd     string `form:"cmd" json:"cmd" xml:"cmd"  binding:"required"`
	Input   string `form:"input" json:"input" xml:"input" `
}
func ProcessRobotPublish( c *gin.Context ) {
	var inputMsg InputMsg
	if err := c.ShouldBindJSON(&inputMsg); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
	}
	robotMgr := robot.GetRobotMgr()
	robotFile := inputMsg.RobotId + ".json"
	_, err := robot.PulishRobot(robotMgr,inputMsg.RobotId,robotFile)
	if (nil == err){
		c.JSON(http.StatusOK,gin.H{"code":"200","message":"Load Ok!"})
	}else{
		c.JSON(http.StatusOK,gin.H{"code":"600","error":err})
	}
}

func ProcessIvr( c *gin.Context ) {
	var inputMsg InputMsg
	if err := c.ShouldBindJSON(&inputMsg); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	/*
		1,Get robotMgr
		2,Get robotDesc
		3,Create Session,Generate FSM 

	*/
	var replyMsg string
	if (CALLIN == inputMsg.Cmd ){
		robotMgr := robot.GetRobotMgr()
		robotFile := inputMsg.RobotId + ".json"
		newrobot, err := robot.GetRobot(robotMgr,inputMsg.RobotId,robotFile)
		if (nil == err){
			sessionMgr := session.GetSessionMgr()
			newsession :=session.CreateSession(sessionMgr,inputMsg.Uuid,newrobot)
			replyMsg= session.Input(newsession, "$start")
			//replyMsg = nlg.GetResponse(nlg.GetRobotNlg(nlg.GetNlgMgr(),"0001"),"welcome")
			fmt.Printf("replyMsg %s\n", replyMsg)
			fmt.Printf("Catch callin event\n")
			c.JSON(http.StatusOK,gin.H{"code":"200","message":replyMsg})
		}else{
			c.JSON(http.StatusOK,gin.H{"code":"600","error":err})
		}
	}else if (CHATTING==inputMsg.Cmd){
		robotMgr := robot.GetRobotMgr()
		sessionMgr := session.GetSessionMgr()
		newrobot,err := robot.GetRobot(robotMgr,"test","test.json")
		if (nil == err){
			newsession := session.GetSession(sessionMgr,inputMsg.Uuid)
			index := robot.GetStateIndex(newrobot,session.GetSessionCurState(newsession))
			fmt.Printf("Index :%d\n",index)
			nlures := robot.MatchCond(inputMsg.Input,newrobot,index)
			replyMsg= session.Input(newsession, nlures)
			//replyMsg = nlg.GetResponse(nlg.GetRobotNlg(nlg.GetNlgMgr(),"0001"),curPoint)
			fmt.Printf("NLU res %s, replyMsg %s\n",nlures, replyMsg)
			//call nlu to understand it and match event
			fmt.Printf("Catch chatting event\n")
			c.JSON(http.StatusOK,gin.H{"code":"200","message":replyMsg})
		}else{
			c.JSON(http.StatusOK,gin.H{"code":"600","error":err})
		}
	}else if(CALLEND==inputMsg.Cmd){
		sessionMgr := session.GetSessionMgr()
		session.ReleaseSession(sessionMgr,inputMsg.Uuid)
		c.JSON(http.StatusOK,gin.H{"code":"200","message":"Release Session OK"})
		fmt.Printf("Catch callend event\n")
	}

	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("%d,%d,%d,%d\n", m.HeapSys,  m.HeapAlloc,
		m.HeapIdle, m.HeapReleased)
}


